package view.macrogui
{
	import events.WorldEvent;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import model.macro.Player;
	import model.macro.Territory;
	import model.macro.Unit;
	import model.macro.World;
	import view.macro.MapSprite;
	import view.macro.TerritorySprite;
	
	/**
	 */
	public class MacroHUD extends Sprite
	{
		private var world:World;
		private var map:MapSprite;
		private var panels:Vector.<Panel>;
		private var sidebar:SideBar;
		private var top_panel:TopPanel;
		
		/**
		 *
		 * @param	world
		 * @param	map
		 */
		public function MacroHUD(world:World, map:MapSprite)
		{
			this.map = map;
			this.world = world;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		/**
		 *
		 * @param	e
		 */
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.ENTER_FRAME, update);
			world.addEventListener(WorldEvent.TURN_BEGIN, onTurnBegin);
			world.addEventListener(WorldEvent.CHANGE_GOLD, onChangeGold);
			world.addEventListener(WorldEvent.TERRITORY_SELECTED, onTerritorySelected);
			world.addEventListener(WorldEvent.UNIT_SELECTED, onUnitSelected);
			world.addEventListener(WorldEvent.UNIT_DESELECTED, onUnitDeselected);
			world.addEventListener(WorldEvent.ALL_UNITS_DESELECTED, onAllUnitsDeselected);
			world.addEventListener(WorldEvent.UNITS_MOVED, onUnitMoved);
			
			addEventListener(WorldEvent.UNIT_SELECTED, forwardEvent);
			addEventListener(WorldEvent.UNIT_DESELECTED, forwardEvent);
			addEventListener(WorldEvent.ALL_UNITS_DESELECTED, forwardEvent);
			addEventListener(WorldEvent.TERRITORY_SELECTED, forwardEvent);
			addEventListener(WorldEvent.TERRITORY_RIGHT_CLICKED, forwardEvent);
			
			panels = new Vector.<Panel>();
			
			sidebar = new SideBar();
			addChild(sidebar);
			sidebar.update(world.current_player, null);
			
			top_panel = new TopPanel();
			top_panel.update(world.current_player, world);
			addChild(top_panel);
		}
		
		private function onUnitMoved(e:WorldEvent):void
		{
			sidebar.redraw();
		}
		
		private function onUnitSelected(e:WorldEvent):void
		{
			if ((e.data as Unit).owner == world.current_player)
			{
				sidebar.onUnitSelected(e);
			}
		}
		
		private function onUnitDeselected(e:WorldEvent):void
		{
			sidebar.onUnitDeselected(e);
		}
		
		private function onAllUnitsDeselected(e:WorldEvent):void
		{
			sidebar.onAllUnitsDeselected(e);
		}
		
		private function onTerritorySelected(e:WorldEvent):void
		{
			for each (var territory:Territory in world.territories)
			{
				map.getTerritorySprite(territory).selected = false;
			}
			territory = e.data as Territory;
			
			sidebar.update(world.current_player, territory);
			
			var sprite:TerritorySprite = map.getTerritorySprite(territory);
			sprite.selected = true;
		}
		
		private function addPanel(panel:Panel):Panel
		{
			panels.push(panel);
			addChild(panel);
			return panel;
		}
		
		private function removePanel(panel:Panel):Panel
		{
			panels.splice(panels.indexOf(panel), 1);
			panel.close();
			return panel;
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			if (child is Panel)
			{
				panels.splice(panels.indexOf(child as Panel), 1);
			}
			return super.removeChild(child);
		}
		
		private function removeAllPanels():void
		{
			for (var i:int = panels.length - 1; i >= 0; i--)
			{
				removePanel(panels[i]);
			}
		}
		
		private function forwardEvent(e:Event):void
		{
			world.dispatchEvent(e);
		}
		
		/**
		 * Called every frame.
		 */
		public function update(e:Event = null):void
		{
			for (var i:int = numChildren - 1; i >= 0; i--)
			{
				var child:Object = getChildAt(i);
				if (child is Updatable)
				{
					if ((child as Updatable).readyToRemove())
					{
						removeChildAt(i);
					}
					else
					{
						(child as Updatable).update();
					}
				}
			}
		}
		
		private function onTurnBegin(e:WorldEvent):void
		{
			for each (var territory:Territory in world.territories)
			{
				map.getTerritorySprite(territory).selected = false;
			}
			sidebar.update(world.current_player, null);
			top_panel.update(world.current_player, world);
		}
		
		private function onChangeGold(e:WorldEvent):void
		{
			if ((e.data[0] as Player) == world.current_player)
			{
				//addChild(new GoldChangeSprite(e.data[1]));
			}
			top_panel.redraw();
			sidebar.redraw();
		}
		
		private function onNextTurn(e:MouseEvent):void
		{
			world.nextTurn();
		}
	
	}

}